import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'

import { useMenuStore } from '../stores/menu'

//   在路由中不能够使用pinia组件
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'home',
      component: HomeView,
      children: [
        {
          path: '/sys',
          name: 'sys',
          component: () => import('../components/sys/SysView.vue')
        },
        {
          path: '/preticket',
          name: 'preticket',
          component: () => import('../components/ticket/PreticketView.vue')
        },
        {
          path: '/role',
          name: 'role',
          component: () => import('../components/role/RoleView.vue')
        }
      ]
    },
    {
      path: '/about',
      name: 'about',
      // route level code-splitting
      // this generates a separate chunk (About.[hash].js) for this route
      // which is lazy-loaded when the route is visited.
      component: () => import('../views/AboutView.vue')
    }
  ]
})

router.beforeEach((to, from) => {
  // 在路由守卫中实例化Pinia
  const menuStore = useMenuStore()
  menuStore.setCurrentpath(to)
  // ...
  // 返回 false 以取消导航,返回true ，按既定路线执行；
  return true
  // 将用户重定向到登录页面
  // return { name: 'Login' }  // 改变目标
})

export default router
